package com.ruoyi.scan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.scan.domain.bo.ApplyBo;
import com.ruoyi.scan.domain.vo.ApplyVo;
import com.ruoyi.scan.domain.Apply;
import com.ruoyi.scan.mapper.ApplyMapper;
import com.ruoyi.scan.service.IApplyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 入驻申请Service业务层处理
 *
 * @author xie
 * @date 2022-09-06
 */
@RequiredArgsConstructor
@Service
public class ApplyServiceImpl implements IApplyService {

    private final ApplyMapper baseMapper;

    /**
     * 查询入驻申请
     */
    @Override
    public ApplyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询入驻申请列表
     */
    @Override
    public TableDataInfo<ApplyVo> queryPageList(ApplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Apply> lqw = buildQueryWrapper(bo);
        Page<ApplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询入驻申请列表
     */
    @Override
    public List<ApplyVo> queryList(ApplyBo bo) {
        LambdaQueryWrapper<Apply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Apply> buildQueryWrapper(ApplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Apply> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, Apply::getDeptId, bo.getDeptId());
        lqw.eq(bo.getUserId() != null, Apply::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), Apply::getAppid, bo.getAppid());
        lqw.eq(bo.getUid() != null, Apply::getUid, bo.getUid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Apply::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), Apply::getTel, bo.getTel());
        lqw.eq(StringUtils.isNotBlank(bo.getWx()), Apply::getWx, bo.getWx());
        lqw.eq(StringUtils.isNotBlank(bo.getApplyType()), Apply::getApplyType, bo.getApplyType());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), Apply::getCompany, bo.getCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getLegalPerson()), Apply::getLegalPerson, bo.getLegalPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getLpTel()), Apply::getLpTel, bo.getLpTel());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessLicense()), Apply::getBusinessLicense, bo.getBusinessLicense());
        lqw.eq(StringUtils.isNotBlank(bo.getFoodLicense()), Apply::getFoodLicense, bo.getFoodLicense());
        lqw.eq(StringUtils.isNotBlank(bo.getCateringLicense()), Apply::getCateringLicense, bo.getCateringLicense());
        lqw.eq(StringUtils.isNotBlank(bo.getOthers()), Apply::getOthers, bo.getOthers());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Apply::getAddress, bo.getAddress());
        lqw.eq(bo.getLongitude() != null, Apply::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, Apply::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getImgs()), Apply::getImgs, bo.getImgs());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCheck()), Apply::getIsCheck, bo.getIsCheck());
        lqw.eq(StringUtils.isNotBlank(bo.getRejectItem()), Apply::getRejectItem, bo.getRejectItem());
        lqw.eq(StringUtils.isNotBlank(bo.getRejectReson()), Apply::getRejectReson, bo.getRejectReson());
        lqw.eq(bo.getCheckUid() != null, Apply::getCheckUid, bo.getCheckUid());
        lqw.eq(bo.getAgreeTime() != null, Apply::getAgreeTime, bo.getAgreeTime());
        String searchValue=bo.getSearchValue();
        if(StringUtils.isNotEmpty(searchValue)){
            lqw.and(w->w
                        .or()
                        .like(Apply::getDeptId,searchValue)

                        .or()
                        .like(Apply::getUserId,searchValue)

                        .or()
                        .like(Apply::getAppid,searchValue)

                        .or()
                        .like(Apply::getUid,searchValue)

                        .or()
                        .like(Apply::getName,searchValue)

                        .or()
                        .like(Apply::getTel,searchValue)

                        .or()
                        .like(Apply::getWx,searchValue)

                        .or()
                        .like(Apply::getApplyType,searchValue)

                        .or()
                        .like(Apply::getCompany,searchValue)

                        .or()
                        .like(Apply::getLegalPerson,searchValue)

                        .or()
                        .like(Apply::getLpTel,searchValue)

                        .or()
                        .like(Apply::getBusinessLicense,searchValue)

                        .or()
                        .like(Apply::getFoodLicense,searchValue)

                        .or()
                        .like(Apply::getCateringLicense,searchValue)

                        .or()
                        .like(Apply::getOthers,searchValue)

                        .or()
                        .like(Apply::getAddress,searchValue)

                        .or()
                        .like(Apply::getLongitude,searchValue)

                        .or()
                        .like(Apply::getLatitude,searchValue)

                        .or()
                        .like(Apply::getImgs,searchValue)

                        .or()
                        .like(Apply::getIsCheck,searchValue)

                        .or()
                        .like(Apply::getRejectItem,searchValue)

                        .or()
                        .like(Apply::getRejectReson,searchValue)

                        .or()
                        .like(Apply::getCheckUid,searchValue)

//                        .or()
//                        .like(Apply::getAgreeTime,searchValue)

            );
        }
        return lqw;
    }

    /**
     * 新增入驻申请
     */
    @Override
    public Boolean insertByBo(ApplyBo bo) {
        Apply add = BeanUtil.toBean(bo, Apply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改入驻申请
     */
    @Override
    public Boolean updateByBo(ApplyBo bo) {
        Apply update = BeanUtil.toBean(bo, Apply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Apply entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除入驻申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
